﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DeliveryService
{
    public class BLFood
    {
        public MFoods SearchFoodsByFoodId(int foodId)
        {
            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {
                var query = db.Foods.Where(s => s.Foods_Id == foodId)
                                    .Select(s => new MFoods
                                    {
                                        FoodId = s.Foods_Id,
                                        FoodName = s.Foods_Name,
                                        Price = s.Foods_Price.HasValue ? (double)s.Foods_Price.Value : 0,
                                        RestaruantAddress = s.Restaurant.Restaurant_Address ?? "",
                                        RestaurantName = s.Restaurant.Restaurant_Name,
                                        IsBusy = s.Restaurant.IsBusy.HasValue ? (bool)s.Restaurant.IsBusy.Value : false,
                                        FoodPicture = s.Foods_Pic ?? ""
                                    });
                return query.FirstOrDefault();
            }
        }

        public List<MFoods> SearchFoodsByFoodName(string foodName)
        {
            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {
                var query = db.Foods.Where(s => s.Foods_Name.Contains(foodName))
                                    .Select(s => new MFoods
                                    {
                                        FoodId = s.Foods_Id,
                                        FoodName = s.Foods_Name,
                                        Price = s.Foods_Price.HasValue ? (double)s.Foods_Price.Value : 0,
                                        RestaruantAddress = s.Restaurant.Restaurant_Address ?? "",
                                        RestaurantName = s.Restaurant.Restaurant_Name,
                                        IsBusy = s.Restaurant.IsBusy.HasValue ? (bool)s.Restaurant.IsBusy.Value : false,
                                        FoodPicture = s.Foods_Pic ?? ""
                                    });
                return query.ToList();
            }
            /*
                var lamda = db.Students.Where(s => s.name == "zhangsan")
                                       .Select(s => new MSchool 
                                                    { 
                                                        SchoolName = s.School.school_name 
                                                    });

                var query = from s in db.Students
                            join sc in db.Schools
                            on s.School_ID equals sc.school_Id
                            where s.name == "zhangsan"
                            select new MSchool
                            {
                                SchoolName = sc.school_name
                            };
            */
        }
        public List<MFoods> SearchFoodByFoodPrice(double fromPrice, double toPrice)
        {
            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {
                var query = db.Foods.Where(s => s.Foods_Price.HasValue && (double)s.Foods_Price.Value >= fromPrice && (double)s.Foods_Price.Value <= toPrice)
                                  .Select(s => new MFoods
                                  {
                                      FoodId = s.Foods_Id,
                                      FoodName = s.Foods_Name,
                                      Price = (double)s.Foods_Price.Value,
                                      RestaruantAddress = s.Restaurant.Restaurant_Address ?? "",
                                      RestaurantName = s.Restaurant.Restaurant_Name,
                                      IsBusy = s.Restaurant.IsBusy.HasValue ? (bool)s.Restaurant.IsBusy.Value : false,
                                      FoodPicture = s.Foods_Pic ?? ""
                                  });
                return query.ToList();

            }
        }
        public List<MFoods> SearchFoodsByFoodCategory(string categoryName)
        {
            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {
                var query = db.Foods.Where(s => s.FoodsCategory.FoodsCategory_Name.Contains(categoryName))
                                     .Select(s => new MFoods
                                     {
                                         FoodId = s.Foods_Id,
                                         FoodName = s.Foods_Name,
                                         Price = s.Foods_Price.HasValue ? (double)s.Foods_Price.Value : 0,
                                         RestaruantAddress = s.Restaurant.Restaurant_Address ?? "",
                                         RestaurantName = s.Restaurant.Restaurant_Name,
                                         FoodPicture = s.Foods_Pic ?? "",
                                         IsBusy = s.Restaurant.IsBusy.HasValue ? (bool)s.Restaurant.IsBusy.Value : false
                                     });
                return query.ToList();
            }
        }
        public List<MFoods> SearchFoodsByRestaurantAndFoodCategory(string restaurantName, string categoryName)
        {
            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {
                var query = db.Foods.Where(s => s.FoodsCategory.FoodsCategory_Name.Contains(categoryName) && s.Restaurant.Restaurant_Name.Contains(restaurantName))
                                     .Select(s => new MFoods
                                     {
                                         FoodId = s.Foods_Id,
                                         FoodName = s.Foods_Name,
                                         Price = s.Foods_Price.HasValue ? (double)s.Foods_Price.Value : 0,
                                         RestaruantAddress = s.Restaurant.Restaurant_Address ?? "",
                                         RestaurantName = s.Restaurant.Restaurant_Name,
                                         IsBusy = s.Restaurant.IsBusy.HasValue ? (bool)s.Restaurant.IsBusy.Value : false,
                                         FoodPicture = s.Foods_Pic ?? ""
                                     });
                return query.ToList();
            }
        }
        public List<MFoodsCategory> SearchFoodsCategoryByRestaurantName(string restaurantName)
        {
            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {
                var query = db.Restaurant_FoodsCategories.Where(s => s.Restaurant.Restaurant_Name.Contains(restaurantName))
                                                         .Select(s => new MFoodsCategory
                                                         {
                                                             FoodsCatgoryId = s.FoodsCategory_Id.HasValue ? (int)s.FoodsCategory_Id : 0,
                                                             FoodsCatgoryName = s.FoodsCategory.FoodsCategory_Name,
                                                             IsBusy = s.Restaurant.IsBusy.HasValue ? (bool)s.Restaurant.IsBusy.Value : false,
                                                             //FoodRestaurantId = s.Restaurant.Restaurant_Id,
                                                             //FoodRestaurantName = s.Restaurant.Restaurant_Name,
                                                             //FoodRestaurantPhone = s.Restaurant.Restaurant_Phone.HasValue ? s.Restaurant.Restaurant_Phone.Value.ToString() : "",
                                                             //FoodRestaurantAdress = s.Restaurant.Restaurant_Address
                                                         });
                return query.ToList();

            }
        }


        public List<MFoods> GetRandomFoods(int getCount)
        {
            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {
                var query = db.Foods.OrderBy(f => db.NewID())
                                    .Select(s => new MFoods
                                      {
                                          FoodId = s.Foods_Id,
                                          FoodName = s.Foods_Name,
                                          FoodPicture = s.Foods_Pic,
                                          Price = s.Foods_Price.HasValue ? (double)s.Foods_Price.Value : 0,
                                          RestaurantName = s.Restaurant.Restaurant_Name,
                                          RestaruantAddress = s.Restaurant.Restaurant_Address,
                                          IsBusy = s.Restaurant.IsBusy.HasValue ? (bool)s.Restaurant.IsBusy.Value : false
                                      });
                if (getCount <= 0) getCount = 1;
                return query.Take(getCount).ToList();
            }
        }

    }
}